package com.haierCamera.customapplication.ui.user.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklActivityAboutBinding;
import com.haierCamera.customapplication.ui.WebViewPublicActivity;
import com.haierCamera.customapplication.ui.login.HZKLLoginActivity;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.UpgradeInfo;

public class HZKLUserAboutActivity extends BaseActivity {
    private HzklActivityAboutBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_about);
        initData();
    }

    private void initData() {
        binding.registerBack.setOnClickListener(v -> finish());
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            String versionName = packageInfo.versionName;
            binding.version.setText(String.format("V%s", versionName));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", "gh_596327d75ef4");
        binding.aboutCheckVersion.setOnClickListener(view -> {
            /***** 获取升级信息 *****/
            UpgradeInfo upgradeInfo = Beta.getUpgradeInfo();
            if (upgradeInfo == null) {
                Toast.makeText(this, "已是最新版本", Toast.LENGTH_SHORT).show();
                return;
            }
            Beta.checkUpgrade(false, false);
        });
        binding.aboutPrivate.setOnClickListener(view -> startActivity(new Intent(HZKLUserAboutActivity.this, WebViewPublicActivity.class)
                .putExtra("title", "隐私政策").putExtra("url", "http://edu-wc.nullehome.com/servicePotocol/#/")
        ));
        binding.aboutXieYi.setOnClickListener(view -> startActivity(new Intent(HZKLUserAboutActivity.this, WebViewPublicActivity.class)
                .putExtra("title", "用户协议").putExtra("url", "http://edu-wc.nullehome.com/servicePotocol/#/")
        ));
        binding.hnyTv.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse("http://www.hainayun.net/");
            intent.setData(content_url);
            startActivity(intent);
        });

        binding.wxService.setOnClickListener(view -> {
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            showDialogText();
        });
    }

    private void showDialogText() {
        new AlertDialog.Builder(HZKLUserAboutActivity.this).setMessage("已复制微信公众号,去关注")
                .setNegativeButton("取消", (dialogInterface, i) ->
                        dialogInterface.dismiss()).setPositiveButton("继续", (dialogInterface, i) -> {

            if (!uninstallSoftware(HZKLUserAboutActivity.this, "com.tencent.mm")) {
                Toast.makeText(HZKLUserAboutActivity.this, "您的设备未安装微信客户端", Toast.LENGTH_SHORT).show();
                return;
            }
            Intent intent = getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
            startActivity(intent);
        }).show();
    }

    private boolean uninstallSoftware(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            if (packageInfo != null) {
                return true;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
}
